package com.skyhigh.web.webservice.controllers;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.skyhigh.web.webservice.domain.Carga;
import com.skyhigh.web.webservice.domain.Contenedor;
import com.skyhigh.web.webservice.domain.ContenidoFacturaList;
import com.skyhigh.web.webservice.domain.Factura;
import com.skyhigh.web.webservice.domain.FacturaList;
import com.skyhigh.web.webservice.services.CargaService;
import com.skyhigh.web.webservice.services.ClienteService;
import com.skyhigh.web.webservice.services.ContenedorService;
import com.skyhigh.web.webservice.services.ContenidoFacturaService;
import com.skyhigh.web.webservice.services.FacturaService;

@Controller
public class FacturasController {
	
	@Autowired
	private FacturaService service;
	@Autowired	
	private ClienteService clienteService;
	@Autowired
	private ContenidoFacturaService contenidoFacturaService;
	@Autowired
	private CargaService cargaService;
	@Autowired
	private ContenedorService contenedorService;
	
	protected final Log logger = LogFactory.getLog(getClass());

	@RequestMapping(value="/buscarFacturas.htm", method = RequestMethod.GET)
	public String desplegarFormularioBusqueda(ModelMap model){	
				
		int totalObjetos = service.obtenerCartidadObjetos(); 
					
		model.addAttribute("totalObjetos", totalObjetos);		
		model.addAttribute("factura", null);		
		model.addAttribute("listaFacturas", null);
		model.addAttribute("listaClientes", clienteService.obtenerObjetos());
		
		return "facturaBuscar";
	}
	
	@RequestMapping(value="/buscarFacturas.htm", method = RequestMethod.POST)
	public String listarBusqueda(ModelMap model, 
			@ModelAttribute("idFactura") Integer idFactura, 
			@ModelAttribute("idCliente") Integer idCliente,		
			BindingResult result, SessionStatus status){
		
		String query = "FROM Factura WHERE idFactura is not Null ";			
		if(idFactura != 0)
			query = query + "AND idFactura = " + idFactura.toString();
		else
			query = query + "AND idFactura LIKE '%' ";	
		
		if(idCliente != 0)
			query = query + "AND idCliente = " + idCliente.toString();
		else
			query = query + "AND idCliente LIKE '%' ";			
		 	
		FacturaList listaFacturas = service.obtenerObjetosByConsulta(query);		
		
		int totalObjetos = service.obtenerCartidadObjetos();
		
		model.addAttribute("totalObjetos", totalObjetos);
		model.addAttribute("parametrosBusqueda", new Carga());
		model.addAttribute("listaFacturas", listaFacturas);		
		model.addAttribute("listaClientes", clienteService.obtenerObjetos());
		
		return "facturaBuscar";
	}
   
	@RequestMapping(value="/verFactura.htm", method = RequestMethod.GET)
	public String verObjeto(ModelMap model, @ModelAttribute("idFactura") Integer idFactura){
		
		Factura factura = service.obtenerObjeto(idFactura);		
		ContenidoFacturaList listaContenidoFactura = contenidoFacturaService.obtenerObjetosByConsulta("FROM ContenidoFactura WHERE idFactura = " + factura.getIdFactura());
		try{						
			Carga carga = cargaService.obtenerObjeto(listaContenidoFactura.getList().get(0).getIdMercancia());
			model.addAttribute("carga", carga);
			model.addAttribute("isCarga", true);
		}catch(Exception e){
			Contenedor contenedor = contenedorService.obtenerObjeto(listaContenidoFactura.getList().get(0).getIdMercancia());
			model.addAttribute("contenedor", contenedor);
			model.addAttribute("isCarga", false);
		}		
				
		model.addAttribute("factura", factura);			
		
		return "facturaVer";
	}
}
